<template>
  <a-layout-header class="layout-header-style">
    <div class="logo" />
    <a-menu mode="horizontal" class="menu" :theme="darkModeComp">
      <!-- ThemeSwitcher 选择框 -->
      <a-menu-item key="theme-switcher" class="theme-switcher">
        <ThemeSwitcher class="dark-theme-switch2" />
      </a-menu-item>

      <!-- 语言切换按钮 -->
      <a-menu-item key="language-switch" class="language-switch">
        <a-button class="switch-language" @click="switchLanguage">
          {{ t("ui.navigation.switchLanguage") }}
        </a-button>
      </a-menu-item>

      <!-- 用户菜单按钮 -->
      <a-menu-item key="user-menu" class="user-menu-item">
        <a-dropdown trigger="click" class="menu-dropdown">
          <a class="ant-dropdown-link"> <UserOutlined /> {{ userName }} </a>
          <template #overlay>
            <a-menu>
              <a-menu-item v-if="isLoggedIn" @click="handleLogout">
                <LogoutOutlined /> {{ $t("ui.navbar.logout") }}
              </a-menu-item>
            </a-menu>
          </template>
        </a-dropdown>
      </a-menu-item>
    </a-menu>
  </a-layout-header>
</template>

<script lang="ts">
import { defineComponent, ref, computed, watch } from "vue";
import { useStore } from "vuex";
import { Layout, Menu, Dropdown } from "ant-design-vue";
import ThemeSwitcher from "./ThemeSwitcher.vue";
import { LogoutOutlined, UserOutlined } from "@ant-design/icons-vue";
import type { MenuTheme } from "ant-design-vue";
import { useI18n } from "vue-i18n"; // 导入 useI18n
import i18n from "../i18n"; // 导入 i18n 实例

export default defineComponent({
  name: "NavBar",
  components: {
    ThemeSwitcher,
    LogoutOutlined,
    UserOutlined,
    "a-layout-header": Layout.Header,
    "a-menu": Menu,
    "a-menu-item": Menu.Item,
    "a-dropdown": Dropdown,
  },
  setup() {
    const store = useStore();
    const { t } = useI18n(); // 使用 useI18n 获取 i18n 实例

    // 计算用户登录状态
    const isLoggedIn = computed(() => store.state.users.isLoggedIn);

    // 计算用户姓名
    const userName = computed(() => {
      return isLoggedIn.value ? store.state.users.userInfo.username : ""; // 仅在登录时获取用户名
    });
    const theme = ref<MenuTheme>("dark");
    const darkModeComp = computed(
      () => store.getters["themeStore/darkModeComp"]
    );

    const handleLogout = () => {
      store
        .dispatch("users/logout")
        .then(() => {
          console.log("用户已登出");
        })
        .catch((error) => {
          console.error("登出失败:", error);
        });
    };

    // 添加这个计算属性来控制用户菜单的显示状态
    const userMenuStyle = computed(() => {
      return {
        display: isLoggedIn.value ? "block" : "none",
      };
    });

    watch(
      () => store.getters["themeStore/darkMode"],
      (newVal) => {
        console.log(`应用于navBar的明暗条？：${newVal}`);
        theme.value = newVal ? "dark" : "light";
      }
    );

    const switchLanguage = () => {
      const newLocale = i18n.global.locale === "en" ? "zh" : "en";
      i18n.global.locale = newLocale;
    };

    return {
      isLoggedIn,
      userName,
      handleLogout,
      darkModeComp,
      switchLanguage,
      theme,
      t,
      userMenuStyle,
    };
  },
});
</script>

<style scoped>
.layout-header-style {
  width: 100%;
  position: fixed; /* 使导航条固定 */
  display: flex; /* 使用 Flexbox 布局 */
  justify-content: space-between; /* 将子元素对齐到两侧 */
  align-items: center; /* 垂直居中 */
  top: 0;
  left: 0;
  right: 0;
  z-index: 1000; /* 确保它在其他内容之上 */
  background-color: var(--bg-color);
}

.logo {
  float: left;
  width: 120px;
  height: 31px;
  margin: 16px 24px 16px 0;
  background-color: var(--bg-color);
}

.menu {
  display: flex; /* 使用 Flexbox 布局 */
  justify-content: flex-end; /* 将子元素对齐到右侧 */
  line-height: 48px;
  padding-top: 8px;
  /* float: right; */
  padding-right: 20px; /* 移到样式类中 */
  background-color: var(--bg-color);
  color: var(--info-color);
  overflow: visible; /* 使菜单内容不被截断 */
  width: calc(100% - 210px);
}

.theme-switcher,
.dark-mode-toggle {
  display: inline-block;
  margin-right: 16px; /* 调整间距 */
}

.switch-language {
  min-width: 120px; /* 根据实际情况调整 */
}
</style>

<style>
/* .user-menu-item { */
/* display: none !important; */
/* } */
.ant-dropdown .ant-dropdown-menu,
.ant-dropdown-menu-submenu .ant-dropdown-menu {
  background-color: var(--bg-color-2);
  color: var(--info-color);
}

.ant-dropdown .ant-dropdown-menu .ant-dropdown-menu-item,
.ant-dropdown-menu-submenu .ant-dropdown-menu .ant-dropdown-menu-item,
.ant-dropdown .ant-dropdown-menu .ant-dropdown-menu-submenu-title,
.ant-dropdown-menu-submenu .ant-dropdown-menu .ant-dropdown-menu-submenu-title {
  background-color: var(--bg-color-2);
  color: var(--info-color);
}
.ant-menu-dark.ant-menu-horizontal > .ant-menu-item-selected {
  /* background-color: var(--bg-color); */
  /* color: var(--info-color); */
  background-color: var(--bg-primary-color);
}
</style>
